package io;

import java.io.*;
import java.util.Scanner;

public class IODemo18 {
    public static void main(String[] args) {
        // 1. 获取要复制的文件和目标位置
        Scanner in = new Scanner(System.in);
        System.out.println("请输入要复制的文件：");
        String srcFileName = in.next();
        System.out.println("请输入目标文件：");
        String destFileName = in.next();

        // 2. 参数校验
        File srcFile = new File(srcFileName);
        if(!srcFile.isFile()){
            System.out.println("输入的要复制的文件有误，请确认");
        }
        File destFile = new File(destFileName);
        if(!destFile.getParentFile().isDirectory()){
            System.out.println("输入的目标文件有误，请确认");
        }

        // 3. 复制
        try(InputStream inputStream = new FileInputStream(srcFile);
            OutputStream outputStream = new FileOutputStream(destFile)){
            while(true){
                byte[] buffer = new byte[1024];
                int n = inputStream.read(buffer);
                if(n == -1){
                    break;
                }
                outputStream.write(buffer, 0, n);
            }

        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
